* Create mover data

	frames reset
	use "${clean_data}/panel_lawyers_clean.dta", clear

	* Restrict to age 40-55
	loc agerange = "40,55" 	
	
	* Define and count moves
	xtset id year
	gen moved = cz90 != L.cz90 if cz90 != . & L.cz90 !=. & inrange(age, `agerange') // Accounting for age-range
	gegen moves = sum(moved), by(id)

	* Creates flags for never movers and single movers
	* (omitted categories - do not observe consecutive years, or observe
	* more than one move)
	gen mover_type = "Never Movers" if moves == 0
	replace mover_type = "Single Movers" if moves == 1
	keep if inlist(mover_type, "Never Movers", "Single Movers")
	drop moves
	
	* Creating relative year variable
	gen temp = year if  moved == 1
	gegen moveyear = max(temp), by(id)
	drop temp

	gen relyear = . 
	replace relyear = 0 if mover_type == "Never Movers"	
	replace relyear = year - moveyear if mover_type == "Single Movers" 
	replace relyear=relyear+16 // +16 to avoid negative factor variables	
	
	gsort id year
	assert !missing(mover_type)
	gisid id year
	gisid id relyear if mover_type == "Single Movers"
	assert relyear==16 if mover_type == "Never Movers"
	
	* Save mover data	
	save "${intermediate_data}/twfe/lawyers_movers.dta", replace
	
	keep if inrange(age, `agerange') 
	keep id year cz90 age ptotinc logptotinc relyear mover_type
	
* Regressions 
	
	* CZ-level income exposure variable
	
	gegen czlogptotinc = mean(logptotinc), by(cz90)

	xtset id year 

	gen temp = czlogptotinc - L.czlogptotinc
	egen incdiff_pos = max(temp), by(id)
	egen incdiff_neg = min(temp), by(id)
	gen incdiff=incdiff_pos if incdiff_pos!=0
	replace incdiff=incdiff_neg if incdiff_neg!=0
	drop temp incdiff_pos incdiff_neg

	
		keep if mover_type == "Single Movers"
		reghdfe logptotinc if !mi(id), a(cz90 age id relyear, savefe) resid
		local N_UR = e(N)
		
		keep if e(sample)
		
		keep logptotinc cz90 id year __hdfe* _reghdfe_resid
		rename (__hdfe1__ __hdfe2__ __hdfe3__ __hdfe4__) (__hdfePLACE__ __hdfeAGE__ __hdfeINDIVID__ __hdfeRELYEAR__)
		g N_UR = `N_UR'
		save "${output}/twfe/geo_twfe/lawyers/IndividFixedEffects_lawyers", replace

		gcollapse logptotinc __hdfe*__ N_UR, by(cz90)
		save "${output}/twfe/geo_twfe/lawyers/CZFixedEffects_lawyers", replace

********* Process results of Two-Way Fixed Effects Regressions 
			
	use "${output}/twfe/geo_twfe/lawyers/IndividFixedEffects_lawyers", clear
	
	local N_UR = `=N_UR[1]'
	
	rename  (__hdfePLACE__ __hdfeAGE__ __hdfeINDIVID__ __hdfeRELYEAR__) (__hdfe1__ __hdfe2__ __hdfe3__ __hdfe4__) 

	preserve
	
		foreach y in 1 2 3 4 {
			qui sum __hdfe`y'__
			gen sd_hdfe`y' = `r(sd)'
			gen CoVar_hdfe`y' = `r(Var)'

			foreach z in 1 2 3 4 {
				if `y' < `z' {
					qui corr __hdfe`y'__ __hdfe`z'__
					gen  Corr_hdfe`y'`z' = r(C)[1,2]

					qui corr __hdfe`y'__ __hdfe`z'__, cov
					gen CoVar_hdfe`y'`z' = `r(cov_12)'
				}
			}
		}

		* Variance of the residual
		qui sum _reghdfe_resid 
		gen CoVar_resid = `r(Var)'

		gcollapse (sd) sd_logptotinc = logptotinc (first) sd_* Corr_* CoVar_*

		gen CoVar_logptotinc = sd_logptotinc * sd_logptotinc

		gen CoVar_recomposed =	CoVar_hdfe1 + CoVar_hdfe2 + CoVar_hdfe3 + CoVar_hdfe4 ///
			+ 2*CoVar_hdfe12 + 2*CoVar_hdfe13 + 2*CoVar_hdfe14 + 2*CoVar_hdfe23 + 2*CoVar_hdfe24 + 2*CoVar_hdfe34 + CoVar_resid

		gen sd_recomposed = sqrt(CoVar_recomposed)

		gen spec = "Person-Year Level"

		save "${temp}/personyear_lawyers", replace

	restore

	gcollapse logptotinc __hdfe* _reghdfe_resid, by(cz90)

	foreach y in 1 2 3 4 {
		qui sum __hdfe`y'__, d
		gen sd_hdfe`y' = `r(sd)'
		gen CoVar_hdfe`y' = `r(Var)'

		foreach z in 1 2 3 4 {
			if `y' < `z' {
				qui corr __hdfe`y'__ __hdfe`z'__
				gen  Corr_hdfe`y'`z' = r(C)[1,2]

				qui corr __hdfe`y'__ __hdfe`z'__, cov
				gen CoVar_hdfe`y'`z' = `r(cov_12)'
			}
		}
	}

	* Variance of the residual
	qui sum _reghdfe_resid 
	gen CoVar_resid = `r(Var)'

	gcollapse (sd) sd_logptotinc = logptotinc (first) sd_* Corr_* CoVar_*

	gen CoVar_logptotinc = sd_logptotinc * sd_logptotinc

	gen CoVar_recomposed =	CoVar_hdfe1 + CoVar_hdfe2 + CoVar_hdfe3 + CoVar_hdfe4  ///
		+ 2*CoVar_hdfe12 + 2*CoVar_hdfe13 + 2*CoVar_hdfe14 + 2*CoVar_hdfe23 + 2*CoVar_hdfe24 + 2*CoVar_hdfe34 + CoVar_resid

	gen sd_recomposed = sqrt(CoVar_recomposed)

	gen spec = "CZ Level"

	append using "${temp}/personyear_lawyers"
	
	reshape long sd_ CoVar_ Corr_, i(spec) j(effect) string
	
	replace effect = subinstr(effect, "12", "Place/Age", .)
	replace effect = subinstr(effect, "13", "Place/Individ", .)
	replace effect = subinstr(effect, "14", "Place/RelYear", .)
	replace effect = subinstr(effect, "23", "Age/Individ", .)
	replace effect = subinstr(effect, "24", "Age/RelYear", .)
	replace effect = subinstr(effect, "34", "Individ/RelYear", .)

	replace effect = subinstr(effect, "1", "Place", .)
	replace effect = subinstr(effect, "2", "Age", .)
	replace effect = subinstr(effect, "3", "Individ", .)
	replace effect = subinstr(effect, "4", "RelYear", .)
	replace effect = subinstr(effect, "hdfe", "", .)
	
	gen id = . 
	replace id = 1 if effect == "logptotinc"
	replace id = 2 if effect == "recomposed"
	replace id = 3 if effect == "Place"
	replace id = 4 if effect == "Age"
	replace id = 5 if effect == "Individ"
	replace id = 6 if effect == "RelYear"
	replace id = 7 if effect == "Place/Age"
	replace id = 8 if effect == "Place/Individ"
	replace id = 9 if effect == "Place/RelYear"
	replace id = 10 if effect == "Age/Individ"
	replace id = 11 if effect == "Age/RelYear"
	replace id = 12 if effect == "Individ/RelYear"

	bys spec: gen temp = CoVar_ if effect == "recomposed"
	egen temp2 = max(temp), by(spec)
	by spec: gen Var_share = CoVar_ / temp2
	
	gen N_UR = `N_UR'
	
	order spec, first 
	gsort -spec id
	drop id temp temp2
	order N_UR, last 
	
	save "${output}/twfe/geo_twfe/lawyers/geotwfe04-earnings_decomp_card_controls_lawyers.dta", replace
	
